//
//  OrderDetailVc.m
//  WGFGlobalHouses
//
//  Created by welldone on 15/5/19.
//  Copyright (c) 2015年 waiguofang. All rights reserved.
//

#import "OrderDetailVc.h"
#import "BdTV.h"
#import "config.h"
#import "OrderDetailHeadCell.h"
#import "OrderDetailCastCell.h"
#import "UIImageView+WebCache.h"
#import "OrderDM.h"
#import "OrderDetailData.h"
#import "NodataCell.h"
#import "NSString+StringHight.h"
#import "OrderDetailFootView.h"
#import "Tools.h"
#import "NSString+category.h"



@interface OrderDetailVc ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UIImageView *headImgView;
@property (nonatomic,strong)UITextView *mobileLab;
@property (nonatomic,strong)UILabel *nameLab,*dolLab,*dwLab;
@property (nonatomic,strong)OrderDM *dm;
@property (nonatomic,strong)OrderDetailData *mainData;
@property (nonatomic,strong)NodataCell *nodataCell;

@property (nonatomic,strong)OrderDetailFootView *footview;
@property (nonatomic,strong)UITableView *tableView;

@end

@implementation OrderDetailVc
@synthesize dm,mainData,nodataCell;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
    
    
    [self refreshData];
    
}



-(void)initView{
    [super initView];
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, PMW, self.view.frame.size.height-NAV_HIGHT-20) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView setSeparatorColor:[UIColor clearColor]];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.navigationItem setHidesBackButton:YES animated:YES];
    
    if (isIOS7) {
        if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
            [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        }
    }
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.tableView setBackgroundColor:UIColorFromRGB(0xf3f3f3)];
    [self.view addSubview:self.tableView];

    
    
    
    BdTV *title = [[BdTV alloc]initWithFrame:CGRectMake(0, 0, PMW, 44)];
    [title.backBtn setTag:11];
    [title.backBtn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
    [[title doneBtn] removeFromSuperview];
    [title setMianTitle:@"订单详情"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:title];
    
    dm = [[OrderDM alloc]init];
   
    self.footview = [OrderDetailFootView createView];
    self.footview.frame = CGRectMake(0, 0, PMW, self.footview.frame.size.height);
    [self.tableView setTableFooterView:self.footview];
    
    
}




-(void)refreshData{
    
    [nodataCell setState:nodataCellStateLoading];
     __weak typeof(self) bself = self;
    [dm refreshDetailData:self.orderCode withCallBack:^(ResulteMod *resultMod) {
       
        
        
        if (resultMod.resltState==ResultNetError) {
            
            [bself.nodataCell setState:nodataCellStateNetError];
            
        }else if (resultMod.resltState==ResultFail){
        
            [bself.nodataCell setCodeErrorTip:resultMod.resultCode];
            
        }else{
            
            bself.mainData = resultMod.dataMod;
            if (bself.mainData == nil) {
                [bself.nodataCell setState:nodataCellStateNodata];
            }
    
        }
         [self upFootView];
        [bself.tableView reloadData];
    
    }];
}



-(void)btnOnClick:(UIButton *)sender{
    if (sender.tag==11) {
        [self.navigationController popViewControllerAnimated:YES];
    }else if (sender.tag==56){
        [self refreshData];
    }
}


#pragma mark tableView delegate
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    if (section==0) {
        return 0;
    }else{
        return 15;
    }
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, tableView.frame.size.width, 15)];
    [footView setBackgroundColor:[UIColor clearColor]];

    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 14, footView.frame.size.width, 1)];
    [line setBackgroundColor:UIColorFromRGB(0xe3e3e3)];
    [footView addSubview:line];
    
    return footView;
}




-(void)upFootView{
    
    if (mainData.state==Progress1) {
        
        [self.footview.phoneText setAttributedText:[NSString makeLineStyle:mainData.buyerMobile color:UIColorFromRGB(0x333333) fontSize:15]];
//        self.footview.phoneText.text = mainData.buyerMobile;
        self.footview.nameLab.text = mainData.buyerName;
        self.footview.moneyLab.text = [NSString stringWithFormat:@"$%@",mainData.intentionPrice];
        
    }else if(mainData.state==Progress8||mainData.state==Progress9||mainData.state==Progress7){
        
        [self.tableView setTableFooterView:nil];
    }else{
        
        [self.footview.phoneText setAttributedText:[NSString makeLineStyle:mainData.buyerMobile color:UIColorFromRGB(0x333333) fontSize:15]];
        //        self.footview.phoneText.text = mainData.buyerMobile;
        self.footview.nameLab.text = mainData.buyerName;
        
        self.footview.moneyLab.hidden = YES;
        self.footview.labe1.hidden = YES;
        self.footview.label2.hidden = NO;
        self.footview.label2.text = [NSString stringWithFormat:@"房源锁定"];
    }

    
    
//    faview.phoneText.text = mainData.buyerMobile;
//    faview.nameLab.text = mainData.buyerName;
//    faview.moneyLab.text = [NSString stringWithFormat:@"$ %@",[Tools getFormNumber:[[NSNumber alloc]initWithInteger:[mainData.intentionPrice integerValue]]]];
    
//    [self.headImgView sd_setImageWithURL:[NSURL URLWithString:mainData.salerImg] placeholderImage:[UIImage imageNamed:DEFAULT_IMG]];
//
//    [self.nameLab setText:mainData.salerName];
//    [self.nameLab sizeToFit];
//    
//    NSMutableAttributedString *attString = [mainData.salerMobile attributedStringFromStingWithFont:self.mobileLab.font withLineSpacing:0];
//    NSRange contentRange = {0, [attString length]};
//    [attString addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange];
//    [self.mobileLab setAttributedText:attString];
//
//    
//    [self.mobileLab sizeToFit];
//    
//    [self.dolLab setText:mainData.moneyRent];
//    [self.dolLab sizeToFit];
//    
//    CGRect buf = self.dolLab.frame;
//    buf.origin.x = self.tableView.frame.size.width-10-self.dolLab.frame.size.width;
//    [self.dolLab setFrame:buf];
//    
//    buf = self.dwLab.frame;
//    buf.origin.x = self.tableView.frame.size.width-10-self.dolLab.frame.size.width-self.dwLab.frame.size.width-5;
//    [self.dwLab setFrame:buf];
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (mainData.itemArray&&mainData.itemArray.count>0) {
    
        if (indexPath.section==0&&indexPath.row==0) {
            return [OrderDetailHeadCell getHight:nil withTableView:tableView];
        }else{
            return [OrderDetailCastCell getHight:nil withTableView:tableView];
        }
    }
    
    return self.tableView.frame.size.height;
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{

    if (mainData.itemArray&&mainData.itemArray.count>0) {
        [self.tableView setScrollEnabled:YES];
        return self.mainData.itemArray.count;
    }else{
        [self.tableView setContentOffset:CGPointMake(0, 0)];
        [self.tableView setScrollEnabled:NO];
        return 1;
    }
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (mainData.itemArray&&mainData.itemArray.count>0) {
        [self.tableView setScrollEnabled:YES];
        
        if (section==0) {
            return [[self.mainData.itemArray objectAtIndex:section] count]+1;
        }else{
            return [[self.mainData.itemArray objectAtIndex:section] count];
        }
    }else{
        [self.tableView setContentOffset:CGPointMake(0, 0)];
        [self.tableView setScrollEnabled:NO];
        return 1;
    }
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    
    if (mainData.itemArray.count==0) {
        if(!nodataCell){
            nodataCell = [[NodataCell alloc] initWithFrame:self.tableView.frame];
            [nodataCell setState:nodataCellStateLoading];
            [[nodataCell refreshBtn]setTag:56];
            [nodataCell.refreshBtn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        return nodataCell;
    }

    
    
    if (indexPath.section==0&&indexPath.row==0) {
        OrderDetailHeadCell *cell = [OrderDetailHeadCell cellWithTableView:tableView];
        [cell setData:mainData];
        return cell;
        
    }else if(indexPath.section==0){
    
        OrderDetailCastCell *cell = [OrderDetailCastCell cellWithTableView:tableView];
        [cell setData:[[mainData.itemArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row-1]];
        return cell;
    
    }else{
    
        OrderDetailCastCell *cell = [OrderDetailCastCell cellWithTableView:tableView];
        [cell setData:[[mainData.itemArray objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]];
        return cell;
    }
}


@end
